<?php
if (!defined('IN_WONDERSHARE'))
{
	die('Hacking attempt');
}
require('./classes/module/module_products_page.php');
class ProductsPageControl
{
	var $products_page_m;
	function __construct()
	{
		$this->products_page_m = new ProductsPageModule();
	}
	/**
	* 根据ws产品id得到产品名称
	*
	* @param   int         $ws_product_id
	*
	* @return  array
	*/
	function  get_product_name($ws_product_id)
	{
		$sql = "SELECT product.product_name FROM won_product  LEFT JOIN product 
		ON product.product_id = won_product.won_product_id  WHERE won_product.id 
		= '$ws_product_id' AND won_product.won_product_delete = 0";
		return $GLOBALS['db']->getOne($sql);
	}
	/**
	* 批量插入修改特点信息
	*
	* @param   string         $str          字符串
	* @param   string        $product_id    商品id
	* @param   string        $type          操作类型
	* @return  int
	*/
	function  insert_feature_info($str,$product_id,$type)
	{
		if($type == 'add')
		{
			$sql ="INSERT INTO feature  (product_id  ,feature_title  ,feature_pic_min ,feature_pic_max 
			,feature_intro,feature_type ,feature_order )  VALUES $str ";
		}elseif($type == 'update')
		{
			$sql = $str;	
		}
		return $this->products_page_m->change_info($sql);
	}

	/**
	* 得到数据提交条件
	*
	* @param   int        $is_recommend     是否删除
	* @param   int        $state            是否发布
	* @param   int        $is_delete        是否删除
	* @param   int        $page_title       标题部分名称
	* @return  string
	*/
	function handle_post($is_recommend,$state,$is_delete,$page_title)
	{ 
		$where_str = '';
		if(!empty($is_recommend))
		{
			$is_recommend = (int)$is_recommend == 2?0:(int)$is_recommend ;
			$where_str.=' AND page_is_recommend = '.$is_recommend;
		}
		if(!empty($state))
		{
			$state = (int)$state == 2?0:(int)$state ;
			$where_str.=' AND page_state  = '.$state;
		}
		
		if(!empty($page_title))
		{
			$page_title = trim($page_title);
			$where_str.=" AND page_title  LIKE '%".$page_title."%' ";
		}
		return  $where_str;
	}
	/**
	* 得到数据提交条件返回url参数
	*
	* @param   int        $is_recommend     是否删除
	* @param   int        $state            是否发布
	* @param   int        $is_delete        是否删除
	* @param   int        $page_title       标题部分名称
	* @return  string
	*/
	function get_url_par($is_recommend,$state,$is_delete,$page_title)
	{ 
		$where_str = '&url_page=1';
		if(!empty($is_recommend))
		{
			$where_str.='&is_recommend='.(int)$is_recommend;
		}
		if(!empty($state))
		{
			$where_str.='&state='.(int)$state;
		}
		if(!empty($page_title))
		{
			$page_title = trim($page_title);
			$where_str.='&page_title='.urlencode($page_title);
		}
		return  $where_str;
	}

	/**
	* 得到分页相关信息
	*
	* @param   int        $page             页码
	* @param   int        $pagesize         取多少条记录
	* @param   int        $products_id      产品id
	* @return  array
	*/
	function get_url_info($page,$pagesize,$products_id = 0)
	{
		$url_str = '';
		$where_str = empty($products_id)?'':' AND  product_id = '.$products_id;
		$sql = "SELECT COUNT(*) AS total FROM product_page  WHERE page_delete  = 0 $where_str  ";
		$page_info = get_page_info($sql,$pagesize,$page);
		$page_url = get_page_url($page,$page_info['page_num'],$url_str);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}
	/**
	* 得到列表或者其它的产品页面信息
	*
	* @param   string     $type             类型（list为列表 不带搜索 而 list_s 在列表 带有搜索,edit代表为编辑）
	* @param   int        $page_id          页面id
	* @param   int        $start            从那行开始
	* @param   int        $pagesize         取多少条记录
	* @param   int        $products_id      ws产品id
	* @return  array
	*/
	function  get_products_page_info($type,$page_id,$start = 0,$pagesize = 10,$products_id = 0 )
	{
		if($type == 'list')
		{
			$where_str = empty($products_id)?'':' AND  page.product_id = '.$products_id;	
			$sql = "SELECT page.page_id ,page.article_id ,page.page_title ,att.name,page.page_update_user,
			page.sort,page.page_update_time  FROM product_page  page  LEFT JOIN product_attributes att ON
			page.article_id = att.id  WHERE page.page_delete  = 0 $where_str  ORDER BY page.sort  ASC  LIMIT
			$start,$pagesize ";
		}elseif($type == 'edit')
		{
			$sql = "SELECT page_id  ,page_title    ,page_click ,page_state ,page_is_recommend ,page_delete   
			,page_content FROM product_page   WHERE  page_id = $page_id";
		
		}
		return $this->products_page_m->get_products_page_info($sql,$type);
	
	}
	/**
	* ajax改变值
	*
	* @param   int      $page_id         页面id
	* @param   int      $change_value    值
	* @param   int      $field           要改变的数据字段
	* @return  int
	*/
	function  change_info($page_id ,$change_value,$field)
	{
		$sql = "UPDATE product_page   SET $field ='$change_value',page_update_time  = '".$_SERVER['REQUEST_TIME']."',
		page_update_user  ='".$_SESSION['AName']."',modify_status = 1 WHERE page_id  = $page_id  "; 
		return $this->products_page_m->change_info($sql);
	}
	/**
	* 修改 
	*
	* @param   int           $page_id          产品页面主键
	* @param   string        $title            产品页面标题
	
	* @param   string        $content          产品页面内容
	* @return  int
	*/
	function update_products_page($page_id,$title,$content )
	{
		$sql = "UPDATE product_page SET page_title ='$title',modify_status = 1 ,page_update_time  = '".$_SERVER['REQUEST_TIME']."',
		page_update_user  ='".$_SESSION['AName']."', page_content = '$content'	WHERE page_id  = $page_id ";
		
		return $this->products_page_m->change_info($sql);
	}


}
?>